package cn.com.dhcc.ddi.datamapping.dao;

import cn.com.dhcc.app.core.dao.BaseDao;
import cn.com.dhcc.app.core.exception.DaoException;
import cn.com.dhcc.app.core.handler.modulehandler.WhereCondition;
import cn.com.dhcc.app.pub.CM;
import cn.com.dhcc.ddi.datamapping.vo.FuncMapping;
import cn.com.dhcc.ddi.datamapping.vo.wapper.FuncMappingRowWapper;
import cn.com.dhcc.ddi.task.service.transform.FunctionAction;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @日期：2016/12/2 15:19
 * @作者：张洪涛
 * @版权所有：东华软件
 * @版本：1.0
 */
@Repository
public class FuncMappingDao extends BaseDao{
	/**
	 * 获取函数映射
	 * @param tableName 所属表名
	 * @param fxActOnType 作用对象类型 1表名、2字段名、3字段值
	 * @return
	 * @throws DaoException
	 */
	public List<FuncMapping> getFuncMappingList(String mappingId, String tableName, String fxActOnType, String actObj) throws DaoException {
		String sql = "select t1.*,t2.fx_name_en from "+SI_TABLE("t_ddi_function_mapping") + " t1 left join "+SI_TABLE("t_ddi_fx_identify")+" t2 on t1.fx_id=t2.id   where t1.mapping_id= ? and t1.table_name=? and t1.fx_act_on_type=? and t1.act_obj=? and t2.iscommon='1' order by t1.fx_exec_order";
		return getDao().listModule("获取函数映射", sql, FuncMapping.class, new Object[]{mappingId, tableName, fxActOnType, actObj});
	}

	/**
	 * 获取函数映射
	 * @param tableName 所属表名
	 * @param fxActOnType 作用对象类型 1表名、2字段名、3字段值
	 * @return
	 * @throws DaoException
	 */
	public List<String> getFuncMappingActobjs(String mappingId, String tableName, String fxActOnType) throws DaoException {
		String sql = "select act_obj from "+SI_TABLE("t_ddi_function_mapping") + " where mapping_id= ? and table_name=? and fx_act_on_type=? order by fx_exec_order";
		return getDao().listSignleStringColumn("获取函数映射", sql, new WhereCondition(), new Object[]{mappingId, tableName, fxActOnType});
	}

	/**
	 *
	 * @Title: batchInsert
	 * @Description: 函数映射批量入库
	 * @param @param funcMappingList
	 * @param @throws DaoException 参数
	 * @return void 返回类型
	 * @throws
	 */
    public int batchInsert(List<Object[]> funcMappingList) throws DaoException{
    	String sql = "insert into "+SI_TABLE("t_ddi_function_mapping")+"(id,mapping_id,table_name,act_obj,fx_id,arg_content,fx_act_on_type,fx_exec_order,mtime)"
    			    +" values(?,?,?,?,?,?,?,?,?)";
    	return CM.getDao().batchInsert("函数映射批量入库", sql, funcMappingList);
    }

	/**
	 *
	 * @Title: getIdsByMappingId
	 * @Description: 根据数据映射id获取函数映射id集合
	 * @param @param mappingId
	 * @param @return
	 * @param @throws DaoException 参数
	 * @return List<String> 返回类型
	 * @throws
	 */
    public List<String> getIdsByMappingId(String mappingId) throws DaoException{
    	String sql = "select id from "+ SI_TABLE("t_ddi_function_mapping") +" where mapping_id=?";
    	return CM.getDao().listSignleStringColumn("根据数据映射id获取函数映射id集合", sql, new WhereCondition(), new Object[]{mappingId});
    }
    
	/**
	 * 
	 * @Title: delete
	 * @Description: 删除函数映射
	 * @param @param id
	 * @param @return
	 * @param @throws DaoException 参数
	 * @return int 返回类型
	 * @throws
	 */
    public int delete(String id) throws DaoException{
    	return CM.getDao().deleteModuleById("删除函数映射", FuncMapping.class, new Object[]{id});
    }

	/**
	 * 获取特殊函数
	 * @param mappingId
	 * @param fxNameEn
	 * @param actType
     * @return
     */
	public List<FuncMapping> getFuncMapping(String mappingId,String tableName,String fxNameEn,String actType) throws DaoException {
		String sql = "select t2.* from "+SI_TABLE("t_ddi_fx_identify") + " t1 left join "+SI_TABLE("t_ddi_function_mapping")+" t2 on t2.fx_id=t1.id";
		WhereCondition searchParameter = new WhereCondition();
		searchParameter.where().eq("t2.mapping_id",mappingId).and().eq("t1.fx_name_en",fxNameEn).and().eq("t2.fx_act_on_type",actType).and().eq("t1.iscommon","0");
		if (tableName!=null && !tableName.isEmpty()) {
			searchParameter.and().eq("t2.table_name",tableName);
		}
		return CM.getDao().list("获取特殊函数", sql, new FuncMappingRowWapper(), searchParameter, new Object[]{});
	}

	/**
	 * 获取特殊函数list
	 * @param mappingId
	 * @param fxNameEn
	 * @param actType
	 * @return
	 */
	public List<FuncMapping> getBathMapping(String mappingId, String tableName, String fxNameEn, String actType) throws DaoException {
		String sql = "select t2.* from "+SI_TABLE("t_ddi_fx_identify") + " t1 left join "+SI_TABLE("t_ddi_function_mapping")+" t2 on t2.fx_id=t1.id";
		WhereCondition searchParameter = new WhereCondition();
		searchParameter.where().eq("t2.mapping_id", mappingId).and().eq("t1.fx_name_en", fxNameEn).and().eq("t2.fx_act_on_type", actType).and().eq("t1.iscommon","0");
		if (tableName!=null && !tableName.isEmpty()) {
			searchParameter.and().eq("t2.table_name",tableName);
		}
		return CM.getDao().list("获取特殊函数", sql, new FuncMappingRowWapper(), searchParameter, new Object[]{});
	}
	/**
	 * 获取批量函数
	 * @param mappingId
	 * @param fxNameEn
	 * @param actType
	 * @return
	 */
	public List<FuncMapping> getBathFuncMps(String mappingId, String tableName, String actType) throws DaoException {
		String sql = "select t2.* from "+SI_TABLE("t_ddi_fx_identify") + " t1 left join "+SI_TABLE("t_ddi_function_mapping")+" t2 on t2.fx_id=t1.id where 1=1 ";
		WhereCondition searchParameter = new WhereCondition();
		searchParameter.and().eq("t2.mapping_id", mappingId).and().eq("t2.fx_act_on_type",actType).and().eq("t1.iscommon","0");
		if ((tableName == null || tableName.isEmpty()) && actType.equals(FuncMapping.ACT_TYPE.ON_TABLE.getCode())){
			sql += ("  and (t2.table_name = '' or t2.table_name is null) ");
		}else{
			searchParameter.and().eq("t2.table_name", tableName);
		}
		searchParameter.and().notIn("t1.fx_name_en",new Object[]{FunctionAction.REMOVE_TABLE, FunctionAction.REMOVE_COL, FunctionAction.ADD_COL});
		return CM.getDao().list("获取特殊函数", sql, new FuncMappingRowWapper(), searchParameter, new Object[]{});
	}
	/**
	 * 
	 * @Title: getList
	 * @Description: 获取函数映射列表信息
	 * @param @param mappingId
	 * @param @return
	 * @param @throws DaoException 参数
	 * @return List<FuncMapping> 返回类型
	 * @throws
	 */
	public List<FuncMapping> getList(String mappingId) throws DaoException{
		String sql = "select * from "+ SI_TABLE("t_ddi_function_mapping") +" where mapping_id=? order by fx_exec_order ";
		return CM.getDao().list("获取函数映射列表信息", sql, new FuncMappingRowWapper(), new Object[]{mappingId});
	}
	
	/**
	 * 
	 * @Title: update
	 * @Description: 更新数据源映射对象
	 * @param @param vo
	 * @param @return
	 * @param @throws DaoException 参数
	 * @return int 返回类型
	 * @throws
	 */
    public int update(FuncMapping vo) throws DaoException{
    	return CM.getDao().updateModuleByIdExecuteNull("更新数据源映射对象", vo);
    }
    
	/**
	 * 
	 * @Title: insert
	 * @Description: 添加数据源映射对象
	 * @param @param vo
	 * @param @return
	 * @param @throws DaoException 参数
	 * @return int 返回类型
	 * @throws
	 */
    public int insert(FuncMapping vo) throws DaoException{
    	return CM.getDao().insertModule("添加数据源映射对象", vo);
    }
    
	/**
	 * 
	 * @Title: getCount
	 * @Description: 根据id获取函数映射的数量
	 * @param @param id
	 * @param @return
	 * @param @throws DaoException 参数
	 * @return int 返回类型
	 * @throws
	 */
    public int getCount(String id) throws DaoException{
    	String sql = "select count(*) from "+ SI_TABLE("t_ddi_function_mapping") +" where id=?";
    	return CM.getDao().queryForInt("根据id获取函数映射的数量", sql, new Object[]{id});
    }
    
}
